package com.redstars.generate.service.impl;

import cn.hutool.core.text.NamingCase;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redstars.generate.entity.SysFieldType;
import com.redstars.generate.entity.SysTableField;
import com.redstars.generate.enums.AutoFillEnum;
import com.redstars.generate.exception.RedstarsException;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.service.SysFieldTypeService;
import com.redstars.generate.utils.PageUtils;
import lombok.AllArgsConstructor;
import com.redstars.generate.convert.SysTableFieldConvert;
import com.redstars.generate.query.SysTableFieldQuery;
import com.redstars.generate.vo.SysTableFieldVo;
import com.redstars.generate.dao.SysTableFieldDao;
import com.redstars.generate.service.SysTableFieldService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 代码生成表字段
 *
 * @author zhouhx 
 * @since 1.0.0 2022-10-05
 */
@Service
@AllArgsConstructor
public class SysTableFieldServiceImpl extends ServiceImpl<SysTableFieldDao, SysTableField> implements SysTableFieldService {

    private final SysFieldTypeService sysFieldType;

    @Override
    public PageResult<SysTableFieldVo> page(SysTableFieldQuery query) {
        LambdaQueryWrapper<SysTableField> wrapper = getWrapper(query);
        PageUtils<SysTableField> pageUtils = new PageUtils<>();
        IPage<SysTableField> page = this.page(pageUtils.getPage(query), wrapper);

        return pageUtils.getPageData(page,SysTableFieldVo.class);
    }

    private LambdaQueryWrapper<SysTableField> getWrapper(SysTableFieldQuery query){
        LambdaQueryWrapper<SysTableField> wrapper = Wrappers.lambdaQuery();

        return wrapper;
    }

    @Override
    public void save(SysTableFieldVo vo) {
        SysTableField entity = SysTableFieldConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysTableFieldVo vo) {
        SysTableField entity = SysTableFieldConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }
    @Override
    public SysTableFieldVo detail(SysTableFieldQuery query) {
        SysTableField detail = this.getById(query.getId());
        if (ObjectUtil.isEmpty(detail)) {
        throw new RedstarsException("记录不存在");
        }
        return BeanUtil.copyProperties(detail,SysTableFieldVo.class);
    }

    @Override
    public void updateTableField(Long tableId, List<SysTableFieldVo> tableFieldList) {
        // 更新字段数据
        int sort = 0;
        for (SysTableFieldVo tableField : tableFieldList) {
            tableField.setSort(sort++);

            this.updateById(SysTableFieldConvert.INSTANCE.convert(tableField));
        }
    }

    @Override
    public void initFieldList(List<SysTableField> tableFieldList) {
        // 字段类型、属性类型映射
        Map<String, SysFieldType> fieldTypeMap = sysFieldType.getMap();
        int index = 0;
        for (SysTableField field : tableFieldList) {
            field.setAttrName(NamingCase.toCamelCase(field.getFieldName()));
            // 获取字段对应的类型
            SysFieldType fieldTypeMapping = fieldTypeMap.get(field.getFieldType().toLowerCase());
            if (fieldTypeMapping == null) {
                // 没找到对应的类型，则为Object类型
                field.setAttrType("Object");
            } else {
                field.setAttrType(fieldTypeMapping.getAttrType());
                field.setPackageName(fieldTypeMapping.getPackageName());
            }

            field.setAutoFill(AutoFillEnum.DEFAULT.name());
            field.setGridItem(true);
            field.setQueryType("=");
            field.setQueryFormType("text");
            field.setFormType("text");
            field.setSort(index++);
        }
    }

    @Override
    public List<SysTableFieldVo> getByTableId(Long tableId) {
        LambdaQueryWrapper<SysTableField> wrapper = new QueryWrapper<SysTableField>().lambda();
        wrapper.eq(SysTableField::getTableId,tableId);
        wrapper.orderByAsc(SysTableField::getSort);
        List<SysTableField> list = this.list(wrapper);
        return SysTableFieldConvert.INSTANCE.convertList(list);
    }

    @Override
    public void deleteBatchTableIds(List<Long> tableIds) {
        LambdaQueryWrapper<SysTableField> wrapper = new QueryWrapper<SysTableField>().lambda()
                .in(SysTableField::getTableId, tableIds);
        boolean remove = remove(wrapper);
        if(!remove){
            throw new RedstarsException("删除失败");
        }
    }

}